//
//  YTFUserModel.swift
//  sinaWeibo
//
//  Created by pro on 16/2/27.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit

//把归档写在模型中

class YTFUserModel: NSObject ,NSCoding{

    var access_token: String?
    
    var expires_in: NSTimeInterval = 0 {
        //didSet 在外界赋值之后调用
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
//    var remind_in: NSTimeInterval = 0
    
    var uid: String?
    
    var expires_date: NSDate?
    
    override init() {
        super.init()
    }
    
    
    //添加用户信息的成员属性
    //头像
    var profile_image_url: String?
    //昵称
    var screen_name: String?
    
    //重载构造方法
    init(dict: [String: NSObject]) {
        
        //调用父类的方法
        super.init()
        
        //KVC
        setValuesForKeysWithDictionary(dict)
    }
    
    //为了防止奔溃，重写一个方法,不添加任何代码，不对奔溃的部分做任何操作
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    func saveUserInfo(fileName: String){
        //获取沙盒路径
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
        
        //拼接文件路径
        let filePath = (path as NSString).stringByAppendingPathComponent(fileName)
        
        //写入偏好设置
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
    }
    
    
    //从沙盒中读解档数据
//    class func readObject(fileName: String) -> YTFUserModel? {
//        //获取沙盒路径
//        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
//        
//        //拼接文件路径
//        let filePath = (path as NSString).stringByAppendingPathComponent(fileName)
//        
//        return NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? YTFUserModel
//    
//    }
    
    class func readObject(fileName: String) -> YTFUserModel? {
        //获取沙盒路径
        let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!

        //拼接文件路径
        let filePath = (path as NSString).stringByAppendingPathComponent(fileName)

        let model = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? YTFUserModel
        
        //如果有值  就和当前时间进行比较
        if let date = model?.expires_date {
            
            if date.compare(NSDate()) == .OrderedDescending{
//                print(model?.profile_image_url ?? "")
//                print(model?.screen_name ?? "")
                return model
            }else{
                return nil
            }
            
        }
        
        return nil
    }
    
    
    //归档和解档
    //归档:把对象以二进制的形式写入文件中
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        
    }
    
    
    //解档:将沙盒中的二进制转化成对象
    //这是一个必须实现的方法，用required修饰
    required init?(coder aDecoder: NSCoder){
        
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    
    }
    
}





